import { AlertController } from 'ionic-angular';
import { AppConfig } from '../app/app.config';
import { Injectable } from '@angular/core';

@Injectable()
export class Fun {
    constructor(public alertCtrl: AlertController) {

    }


    //是否登陆
    static isLogin() {
        var userKey = this.getUser();
        if (!this.isNullOrEmpty(userKey)) {
            return true;
        }
        else {
            return false;
        }
    }


    static setUser(value) {
        this.setCookie(AppConfig.UserKey, value)
    }
    static setcookie(key,value) {
        this.setCookie(key, value)
    }

    static getUser() {
        var value = this.getCookie(AppConfig.UserKey);
        return value;
    }
    static getcookie(key) {
        var value = this.getCookie(key);
        return value;
    }


    static isNullOrEmpty(obj) {
        if (obj == null || obj == "") {
            return true;
        }
        else {
            return false;
        }

    }

    static getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return r[2]; return null;
    }


    static checkPhone(value) {
        if (this.isNullOrEmpty(value) || value.length != 11) {
            return false;
        }
        var reg = /^13[0-9]{9}|15[012356789][0-9]{8}|18[0-9]{9}|(14[57][0-9]{8})|(17[015678][0-9]{8})$/;
        return reg.test(value);
    };

    //设置cookie  
    static setCookie(cname, cvalue) {
        var d = new Date();
        var exdays = 7;
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }


    //获取cookie  
    static getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
        }
        return "";
    }

    //通用弹窗
    showAlert(msg: string) {
        let alert = this.alertCtrl.create({
            title: '温馨提示!',
            subTitle: msg,
            buttons: ['确定']
        });
        alert.present();

    }
    showlogAlert(title,msg) {
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: msg,
            buttons: [{
                text: '关闭',
                handler: data => {
                    // this.navCtrl.pop();
                }
            }]
        });
        alert.present();
    }
    //通用弹窗
    showUpdateAlert(title, msg, confirm, cancel, isMustUpdate) {
        var btns = [];
        if (!isMustUpdate)//不是必须更新
        {
            btns.push({
                text: '下次再说',
                handler: data => {
                    cancel && cancel();
                }
            });

        }
        btns.push({
            text: '前往更新',
            handler: data => {
                confirm && confirm();
            }
        });

        let alert = this.alertCtrl.create({
            title: title,
            subTitle: msg,
            buttons: btns
        });
        alert.present();
    }


    showPrompt(field, msg) {
        let prompt = this.alertCtrl.create({
            inputs: [
                {
                    name: field,
                    placeholder: msg
                },
            ],
            buttons: [
                {
                    text: '取消',
                    handler: data => {
                        console.log('Cancel clicked');
                    }
                },
                {
                    text: '确定',
                    handler: data => {
                        console.log('Saved clicked' + data);
                    }
                }
            ]
        });
        prompt.present();
    }
}




